/*
 * $Id: PictureLoader.java 187 2009-06-09 10:45:32Z meyer@alphatec.de $
 * 
 *
 * Copyright 2009 DeltaDesk
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package deltadesk.apps.pictureviewer.io;

import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.imageio.ImageIO;

import deltadesk.apps.pictureviewer.data.Picture;
import deltadesk.apps.pictureviewer.util.concurrent.BaseStoppableThread;

/**
 * PictureLoader
 * 
 * @author Andreas Meyer
 * @version $Revision: 187 $
 */
public class PictureLoader extends BaseStoppableThread implements RejectedExecutionHandler {
	
	/** The executor for picture loading. */
	private ThreadPoolExecutor pictureLoader;
	
	/** The picture array. */
	private final Picture[] pictures;


	/**
	 * Construct the picture loader.
	 */
	public PictureLoader(final Picture[] pictures) {
		
		this.pictures = pictures;
		
		pictureLoader = new ThreadPoolExecutor(
				5,   // core pool size
                25,  // max pool size
                1,  // keep alive time
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(1000),
                this);
		
		final Picture pic = pictures[0];
		try {
			pic.setImage(ImageIO.read(pic.getSource()));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see java.lang.Thread#run()
	 */
	@Override
	public void run() {
		
		for(final Picture pic : pictures) {
			
			pictureLoader.execute(new Runnable() {

				public void run() {
					// TODO Auto-generated method stub
					try {
						pic.setImage(ImageIO.read(pic.getSource()));
						System.out.println("Read " + pic.getSource());
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
			});

			if(!isRunning()) {
				break;
			}
		} // end for

		System.out.println("End Read");
	}

	/**
	 * @see deltadesk.apps.pictureviewer.util.concurrent.BaseStoppableThread#shutdown()
	 */
	@Override
	public synchronized void shutdown() {
		
		super.shutdown();
		pictureLoader.shutdownNow();
	}

	/**
	 * @see java.util.concurrent.RejectedExecutionHandler#rejectedExecution(java.lang.Runnable, java.util.concurrent.ThreadPoolExecutor)
	 */
	public void rejectedExecution(Runnable r, ThreadPoolExecutor tpe) {
		System.out.println(tpe.getTaskCount());
		
	}

}
